using APP.HELPER.Data.Helper;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Extra.Common;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Extra.Data.Extra.Forms
{
    public class xfmExecuteDatabase : XtraForm
    {
        private SimpleButton btnClose;
        private SimpleButton btnHelp;
        private SimpleButton btnStart;
        private IContainer components = null;
        private LabelControl lblDetail;
        private LabelControl lblMessage;
        private PictureBox pictureBox1;
        private ButtonEdit txtFilePath;

        public xfmExecuteDatabase()
        {
            xfmDatabaseConfig config = new xfmDatabaseConfig();
            config.Logined += new xfmDatabaseConfig.LoginedEventHander(this.xfm_Logined);
            config.Cancel += new xfmDatabaseConfig.CancelEventHander(this.xfm_Cancel);
            config.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string str = "";
            StreamReader reader = new StreamReader(this.txtFilePath.Text);
            SqlHelper helper = new SqlHelper
            {
                CommandType = CommandType.Text
            };
            helper.Extract();
            str = helper.ExecuteNonQuery(reader.ReadToEnd());
            reader.Close();
            if (str != "OK")
            {
                XtraMessageBox.Show("Thất bại!");
            }
            else
            {
                XtraMessageBox.Show("Th\x00e0nh c\x00f4ng!");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xfmExecuteDatabase));
            SerializableAppearanceObject appearance = new SerializableAppearanceObject();
            this.lblMessage = new LabelControl();
            this.pictureBox1 = new PictureBox();
            this.btnHelp = new SimpleButton();
            this.btnClose = new SimpleButton();
            this.btnStart = new SimpleButton();
            this.txtFilePath = new ButtonEdit();
            this.lblDetail = new LabelControl();
            ((ISupportInitialize)this.pictureBox1).BeginInit();
            this.txtFilePath.Properties.BeginInit();
            base.SuspendLayout();
            this.lblMessage.Appearance.Font = new Font("Tahoma", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblMessage.Appearance.ForeColor = Color.SteelBlue;
            this.lblMessage.Appearance.Options.UseFont = true;
            this.lblMessage.Appearance.Options.UseForeColor = true;
            this.lblMessage.AutoSizeMode = LabelAutoSizeMode.Vertical;
            this.lblMessage.Location = new Point(140, 12);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new Size(0x151, 14);
            this.lblMessage.TabIndex = 0;
            this.lblMessage.Text = "Thực thi lệnh SQL";
            this.pictureBox1.Dock = DockStyle.Left;
            this.pictureBox1.Image = Img.bg_update;
            this.pictureBox1.Location = new Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new Size(0x7d, 0x17e);
            this.pictureBox1.TabIndex = 0x26;
            this.pictureBox1.TabStop = false;
            this.btnHelp.Image = (Image)manager.GetObject("btnHelp.Image");
            this.btnHelp.Location = new Point(0x89, 0x15f);
            this.btnHelp.Name = "btnHelp";
            this.btnHelp.Size = new Size(0x49, 0x15);
            this.btnHelp.TabIndex = 0x1d;
            this.btnHelp.Text = "Trợ Gi\x00fap";
            this.btnClose.Image = (Image)manager.GetObject("btnClose.Image");
            this.btnClose.Location = new Point(0x1b9, 350);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(0x54, 0x16);
            this.btnClose.TabIndex = 8;
            this.btnClose.Text = "Bỏ Qua";
            this.btnClose.Click += new EventHandler(this.btnClose_Click);
            this.btnStart.Image = (Image)manager.GetObject("btnStart.Image");
            this.btnStart.Location = new Point(350, 350);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new Size(0x55, 0x16);
            this.btnStart.TabIndex = 7;
            this.btnStart.Text = "Thực Hiện";
            this.btnStart.Click += new EventHandler(this.btnStart_Click);
            this.txtFilePath.Location = new Point(0x89, 0x3d);
            this.txtFilePath.Name = "txtFilePath";
            this.txtFilePath.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Glyph, "", -1, true, true, false, ImageLocation.MiddleCenter, (Image)manager.GetObject("txtFilePath.Properties.Buttons"), new KeyShortcut(Keys.None), appearance, "", "Browse", null, true) });
            this.txtFilePath.Properties.ButtonClick += new ButtonPressedEventHandler(this.txtFilePath_Properties_ButtonClick);
            this.txtFilePath.Size = new Size(0x18a, 0x16);
            this.txtFilePath.TabIndex = 0x27;
            this.txtFilePath.TabStop = false;
            this.lblDetail.Appearance.Options.UseTextOptions = true;
            this.lblDetail.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            this.lblDetail.AutoSizeMode = LabelAutoSizeMode.None;
            this.lblDetail.Location = new Point(140, 0x20);
            this.lblDetail.Name = "lblDetail";
            this.lblDetail.Size = new Size(0x181, 0x1a);
            this.lblDetail.TabIndex = 0x21;
            this.lblDetail.Text = "Chọn file c\x00f3 định dạng mở rộng l\x00e0 *.sql rồi nhấn n\x00fat thực hiện b\x00ean dưới.";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x21f, 0x17e);
            base.Controls.Add(this.txtFilePath);
            base.Controls.Add(this.pictureBox1);
            base.Controls.Add(this.lblDetail);
            base.Controls.Add(this.btnHelp);
            base.Controls.Add(this.btnClose);
            base.Controls.Add(this.btnStart);
            base.Controls.Add(this.lblMessage);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "xfmExecuteDatabase";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Thực thi lệnh SQL";
            ((ISupportInitialize)this.pictureBox1).EndInit();
            this.txtFilePath.Properties.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void txtFilePath_Properties_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Filter = "SQL File(*.sql)|*.sql",
                FilterIndex = 0
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = dialog.FileName;
            }
        }

        private void xfm_Cancel(object sender)
        {
            Application.Exit();
        }

        private void xfm_Logined(object sender, string ConnectString)
        {
            this.InitializeComponent();
        }
    }

}
